﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.Data;
using System.Text;

namespace HQWeb.Procurement
{
    public partial class Scan : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    string PID = Request["ID"].ToString();
                    BindDate(PID);
                }
                if (Request["sign"] != null)
                {
                    if (Request["sign"] == "3")
                    {
                        btnOK.Visible = false;
                        btnNOT.Visible = false;
                        btnRe.Visible = false;
                        notation.ReadOnly = true;
                    }
                    else if (Request["sign"] == "1")
                    {
                        btnCal.Visible = false; 
                    }
                    else if (Request["sign"] == "2")
                    {
                        btnCal.Visible = false;
                    }
                    else if (Request["sign"] == "4")
                    {
                        btnOK.Visible = false;
                        btnNOT.Visible = false;
                        btnCal.Visible = false;
                        notation.ReadOnly = true;
                    }
                }
            }
        }

        protected void BindDate(string PID)
        {
            Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            purchase = bll.GetModel(PID);
            BindState(PID);
            BindMaterial(PID);
            BindBranch(PID);
            lblapplicant.Text = purchase.APPLICANT_NAME;
            this.lbltitle.Text = purchase.TITLE;
            //this.lblnum.Text = purchase.NUM.ToString();
            if (purchase.APPROVER_NAME != "")
            {
                lblapprover.Text = purchase.APPROVER_NAME;
            }
            if (purchase.NOTATION != "")
            {
                notation.Text = purchase.NOTATION;
            }
            lblapprover.Text = purchase.APPLICANT_NAME;
            this.Remark.Text = purchase.REMARK;
            this.applytime.Text = Convert.ToDateTime(purchase.APPLICANT_TIME.ToString()).ToShortDateString();
            this.endtime.Text = Convert.ToDateTime(purchase.END_TIME.ToString()).ToShortDateString();
        }

        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
            DataTable modelList = bll.GetListByPid(id).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralName;
            string strNum;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralName = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();

                strHtml.Append("  <table width=\"58%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:10%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:10%\">");
                strHtml.Append(" <input type=\"text\" readonly=\"readonly\" id=\"n" + i + "\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strMateralName + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                strHtml.Append("申请数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width: 10%\">");
                strHtml.Append("<input type=\"text\" readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                //strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                //strHtml.Append("库存数量：");
                //strHtml.Append("</td>");
                //strHtml.Append("<td align=\"left\">");
                //strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" readonly=\"readonly\" id=\"c" + i + "\" value=\"" + strNum + "\"/>");
                //strHtml.Append(" </td>");
                //strHtml.Append("<td align=\"right\" style=\"width: 10%\" >");
                //strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                //strHtml.Append("</td>");
                strHtml.Append(" <input type=\"text\" id=\"p" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();
            
        }
        #endregion

        #region 绑定状态
        private void BindState(string id)
        {

            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            Model.Procurement.T_PUB_PURCHASE purchaseModel = bll.GetModel(id);
            if (purchaseModel != null)
            {
                string state=purchaseModel.STATE;
                switch (state)
                {
                    case "1": this.lblstate.Text = "待审批"; break;
                    case "2": this.lblstate.Text = "审批未通过"; break;
                    case "3": this.lblstate.Text = "审批通过"; break;
                    default:
                        break;
                }
                return;
            }
        }
        #endregion

        #region  绑定分店
        private void BindBranch(string id)
        {     
            Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            purchase = bll.GetModel(id);
            int branchid = Convert.ToInt32(purchase.BRANCH_ID.ToString());
            Model.SysManage.T_PUB_BRANCH branch = new Model.SysManage.T_PUB_BRANCH();
            BLL.SysManage.T_PUB_BRANCH bllbranch = new BLL.SysManage.T_PUB_BRANCH();
            branch = bllbranch.GetModel(branchid);
            if (branch != null)
            {
                this.lblbranch.Text = branch.BRANCH_NAME;
            }
        }
        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string PID = Request["ID"].ToString();
            Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            purchase = bll.GetModel(PID);
            purchase.APPROVER = Convert.ToInt32(Session["USER_ID"].ToString());
            purchase.APPROVER_NAME = Session["USER_NAME"].ToString();
            purchase.NOTATION = this.notation.Text;
            purchase.STATE = "3";
            bll.Update(purchase);
            Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "采购申请" + purchase.TITLE + " 状态为审批已通过", DateTime.Now);
            Response.Redirect("ApproveList.aspx?userid=" + Request["userid"]);
        }

        protected void btnNOT_Click(object sender, EventArgs e)
        {
            string PID = Request["ID"].ToString();
            Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            purchase = bll.GetModel(PID);
            purchase.APPROVER = Convert.ToInt32(Session["USER_ID"].ToString());
            purchase.APPROVER_NAME = Session["USER_NAME"].ToString();
            purchase.NOTATION = this.notation.Text;
            purchase.STATE = "2";
            bll.Update(purchase);
            Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "采购申请" + purchase.TITLE + " 状态为审批不通过", DateTime.Now);
            Response.Redirect("ApproveList.aspx?userid=" + Request["userid"]);
        }

        protected void btnRe_Click(object sender, EventArgs e)
        {
            Response.Redirect("ApproveList.aspx?userid="+Request["userid"]);
        }

        protected void btnCal_Click(object sender, EventArgs e)
        {
            Response.Redirect("ApplyList.aspx?userid=" + Request["userid"]);
        }

        //#region  绑定审批人
        //private void BindApprover(int id)
        //{
        //    Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
        //    BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
        //    purchase = bll.GetModel(id);
        //    int approverid = int.Parse(purchase.APPROVER.ToString());
        //    Model.SysManage.T_PUB_USER user = new Model.SysManage.T_PUB_USER();
        //    BLL.SysManage.T_PUB_USER blluser = new BLL.SysManage.T_PUB_USER();
        //    user = blluser.GetModel(approverid);
        //    if (user != null)
        //    {
        //        this.lblapprover.Text = user.NAME;
        //    }
        //}
        //#endregion

    }
}